/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class MidpointConstraint extends Node {

    //This class will try and locate itself between these points

    Point a, b, mid;



    float ratio = .5f;
    float mass = 1.f;



    public MidpointConstraint(LineConstraint line, Vector <Node> nodes) {
        this.a = line.a;
        this.b = line.b;

        PVector midT = getMid();
        mid = new Point(midT.x,midT.y,midT.z);

        nodes.add(mid);
    }
    public MidpointConstraint(Point a, Point b, Point c,  Vector <Node> nodes) {
        this.a = a;
        this.b = b;

        this.mid = c;
    }
    private PVector getMid(){
        PVector pos;

        float length;
        PVector toVect = PVector.sub(a.pos,b.pos);
        length = toVect.mag();
        toVect.div(length);
        pos = b.pos.get();
        pos.add(PVector.mult(toVect,ratio*length));
        return pos;
    }

    public void physics() {

        PVector lMid = getMid();
        PVector toLMid = PVector.sub(lMid,mid.pos);


        float length = toLMid.mag();

        if (length != 0){
            toLMid.div(length);

            //move the point
            mid.pos.add(PVector.mult(toLMid,length/2));

            //move the line
            a.pos.sub(PVector.mult(toLMid,length/4));
            b.pos.sub(PVector.mult(toLMid,length/4));
        }
    }

    public void render(int mode) {



    }

    public PVector getCentroid() {
        return new PVector( (b.pos.x - a.pos.x)/2 + a.pos.x,
                            (b.pos.y - a.pos.y)/2 + a.pos.y,
                            (b.pos.z - a.pos.z)/2 + a.pos.z);
    }
    public PVector getScreenSpaceCentroid() {
        float[] ret = GLProject.project((b.pos.x - a.pos.x) / 2 + a.pos.x,
                (b.pos.y - a.pos.y) / 2 + a.pos.y,
                (b.pos.z - a.pos.z) / 2 + a.pos.z);

        return new PVector(ret[0], ret[1], 0);
    }


    public MidpointConstraint(String [] line, Vector <Node> nodes ){

        this((Point)nodes.elementAt(Integer.parseInt(line[1])),
             (Point)nodes.elementAt(Integer.parseInt(line[2])),
             (Point)nodes.elementAt(Integer.parseInt(line[3])),
             nodes);
            mass = Util.strToFlt(line[4]);
    }

    public String pickle(Vector <Node> nodes) {
        return "M," +
                nodes.indexOf(a) + "," +
                nodes.indexOf(b) + "," +
                nodes.indexOf(mid) + "," +
                Float.floatToIntBits(mass) + ",";

    }

    @Override
    public boolean dependsOn(Point p) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
